using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using RFS.Domain.PaiShi.Zuhuo;
using ycx;

namespace ItProto1
{
    public class Zuhuaer
    {
        
        
        // private   int max = 30;
        // private   int min = 10;

        public int Max { get; set; } = 30;
        public int Min { get; set; } = 10;
        
        public string GName { get; set; }

        public override string ToString()
        {
            return $"{GName} ({_zaHuaItems.Count},en:{IsEnouph} event:{IsEven},evc:{EvenItems.Count()} )";
        }

        Dictionary<ulong, ZaHuaItem> _zaHuaItems = new Dictionary<ulong, ZaHuaItem>();

        public Zuhuaer(int min=10, int max=30)
        {
            Min = min;
            Max = max;
        }


        public void AddZaHuaItem(ZaHuaItem za)
        {
            if (IsFull) throw new Exception($"too much for [{GName}]");

            //100--300 10-30z
            GName =  za.GroupKey;
            _zaHuaItems[za.IdBackPointer] = za;
        }

        public bool IsEnouph
        {
            get => _zaHuaItems.Count >= Min;
        }

        public bool IsEven
        {
            get => _zaHuaItems.Count % 2 == 0;
        }


        public IEnumerable<ZaHuaItem> EvenItems
        {
            get
            {
                if (IsEven) return _zaHuaItems.Values;

                return _zaHuaItems.Values.Where(x => x.IdBackPointer != OddItem.IdBackPointer);
            }
        }

        public ZaHuaItem OddItem
        {
            get
            {
                if (IsEven) return default;
                return _zaHuaItems.Values.LastOrDefault();
            }
        }


        public bool IsFull
        {
            get => _zaHuaItems.Count >= Max;
        }

        public IEnumerable<ZaHuaItem> ZahuoItems { get=>_zaHuaItems.Values.ToList(); }
        public int GroupBasedIndex { get; set; } = 0;
    }
    
    
    
}